//
//  SetPasswordView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2024/12/26.
//

import SwiftUI

struct AiSetMemoryView: View {
    @EnvironmentObject var appStorageManager: AppStorageManager //加这个才能刷新监听

    var forceShow = false
    @State private var isShow = false
    
    var body: some View {
        contentView
    }
}

extension AiSetMemoryView {
    var contentView : some View {
        HStack{
            VStack(alignment: .leading, spacing: 18) {
                HStack{
                    Text("记忆设置")
                        .foregroundColor(.bgItemContent)
                        .fontWeight(.bold)
                        .font(.system(size: 14))
                    Spacer()
                    Image(.myIconExpand)
                        .resizable()
                        .frame(width: 15,height: 15)
                        .rotationEffect(isShow ? Angle(degrees: 180) : .zero)
                }.contentShape(Rectangle())// 将整个矩形区域（包括padding）都设置为可点击区域
                 .onTapGesture {
                    withAnimation(Animation.easeInOut(duration: 0.3)) {
                        if !forceShow {
                            isShow.toggle()
                        }
                    }
                }
                
                if isShow{
                    HStack {
                        VStack(alignment:.leading){
                            Text("使用规则")
                                .foregroundColor(.bgItemContent)
                                .font(.system(size: 14))
                            Text("使用之前请仔细阅读规则")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }
                        Spacer()
                        Image(.appIconArrowRight)
                            .resizable()
                            .frame(width: 15,height: 15)
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                            pathManager.jump(type: JumpType.MEMORY_DESC)
                        }
                    HStack {
                        VStack(alignment:.leading){
                            HStack{
                                Text("记忆提取")
                                    .foregroundColor(.bgItemContent)
                                    .font(.system(size: 14))
                                Image(.appIconVip)
                                    .resizable()
                                    .frame(width: 15,height: 15)
                            }
                            Text("不定时的会自动提取重要记忆")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }
                        Spacer()
                        Toggle("", isOn: appStorageManager.$toggleMemory)
                            .foregroundColor(.bgItemContent)
                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                            .font(.system(size: 14))
                            .labelsHidden() // 关键：隐藏 Toggle 的标题标签，彻底取消文字相关布局
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                        }
                    HStack {
                        VStack(alignment:.leading){
                            HStack{
                                Text("记忆注入")
                                    .foregroundColor(.bgItemContent)
                                    .font(.system(size: 14))
                                Image(.appIconVip)
                                    .resizable()
                                    .frame(width: 15,height: 15)
                            }
                            Text("将提取的记忆注入心灵里，相应的会增加回应次数")
                                .foregroundColor(.bgNormalText4)
                                .font(.system(size: 10))
                        }
                        Spacer()
                        Toggle("", isOn: appStorageManager.$toggleMemoryInject)
                            .foregroundColor(.bgItemContent)
                            .toggleStyle(SwitchToggleStyle(tint: themeManager.themeColor))
                            .font(.system(size: 14))
                            .labelsHidden() // 关键：隐藏 Toggle 的标题标签，彻底取消文字相关布局
                        
                    }.padding(.top,5).padding(.bottom,5)
                        .contentShape(Rectangle())
                        .onTapGesture {
                        }
                }
            }
            Spacer()
        }.frame(maxWidth: .infinity).padding(18).background(.bgItem).clipShape(RoundedRectangle(cornerRadius: 15))
            .onChange(of: appStorageManager.toggleMemory) { newValue in
                if newValue {
                    if userManager.isVip() {
                        ActiveAIMemoryExt.instance.checkAIMemory()
                        showTipsDialog(text: "使用之前请仔细阅读规则")
                    } else {
                        AppStorageManager.shared.toggleMemory = false
                        showTipsDialog(text: "该功能是Pro专享")
                    }
             
                }
            }
            .onChange(of: appStorageManager.toggleMemoryInject) { newValue in
                if newValue {
                    if userManager.isVip() {
                        ActiveAIMemoryExt.instance.doMemoryStr()
                        showTipsDialog(text: "使用之前请仔细阅读规则")
                    } else {
                        AppStorageManager.shared.toggleMemoryInject = false
                        showTipsDialog(text: "该功能是Pro专享")
                    }
                }
            }
            .onAppear {
                if forceShow {
                    isShow = true
                }
            }
    }
}

//#Preview {
//    SetPasswordView()
//}

//定义预览提供者
struct AiSetMemoryView_Previews: PreviewProvider {
    static var previews: some View {
        //使用预览共享配置
        configurePreviewEnvironment(for: AiSetMemoryView())
    }
}
